package com.hqyj.javaSpringBoot.modules.test.dao;

import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends JpaRepository<Student,Integer> , JpaSpecificationExecutor<Student>  {
    Student findFirstByNameAndEmail(String name, String email);

    @Query(value="from Student where id =: id")  //自定义HQL语句查询数据库
    Student getStudentById(@Param("id") int id);

    @Query(nativeQuery=true,value="select * from table_student where id =:id")  //自定义HQL语句查询数据库
    Student getStudentByIdForSql(@Param("id") int id);

    @Query(nativeQuery=true, value = "insert into test_student (name, email, age) " +
            "values(:#{#student.name}, :#{#student.email}, :#{#student.age})")
    @Modifying//代表增删改操作
    void insertStudent(@Param("student") Student student);//:#{#student.name}传递对象的获取方式
}
